package unipi.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import unipi.database.Tasks;
import unipi.helpers.HTMLPages;
import unipi.helpers.LoginCookie;
import unipi.helpers.Validator;

public class Blog extends HttpServlet
{
	private static final long serialVersionUID = 1L;
	
	public Blog()
	{
		super();
	}
	
	public void init() throws ServletException
	{
		
	
	}
	
	private String getPage(String uid, int page, String userID, String taskID)
	{
		String content = "<h1>Ιστολόγιο</h1>";
		
		int maxResultsPerPage = 5;
		
		if(taskID != null) if(taskID.equals("none")) taskID = null;
		
		List<Map<String, String>> progressList = Tasks.getProgress(userID, taskID, page, maxResultsPerPage);
		
		if(progressList != null)
		{
			if(progressList.size() == 0)
			{
				content += "Δεν υπάρχουν δημοσιεύσεις ακόμα.";				
			}
			else
			{			
				List<List<String>> progressedTasks = Tasks.getProgressedTasks(userID);		
				
				if(progressedTasks != null)
				{
					content += "<form name=\"filterbytask\" method=\"get\" action=\"Blog\">" +
							"<table><tr><td colspan=\"2\"><input type=\"hidden\" name=\"uid\" value=\"" + userID + "\" /></td></tr>" + 
							"<tr><td>Eργασία: </td>" +
							"<td><div class=\"ddldivstyle\">" + HTMLPages.getDropDownList(progressedTasks, "taskid",
									taskID == null?"none":taskID, "Εμφάνιση Όλων", 
									"onchange=\"document.filterbytask.submit();\"") + "</div>" +
									"</td></tr></table></form><br /><br />";			
				}
				
				
				for(Map<String, String> row : progressList)
				{
					content += "<div class=\"blogentry\">";
					
					content += "<div class=\"blogtitle\"><div class=\"blogtitlewrapper\">" + row.get("title") + "</div></div>";				
					content += "<div class=\"blogmaintop\"></div>";
					content += "<div class=\"blogmainmiddle\">";
					
					content += "<div class=\"blogmainwrapper\">";
					content += "<div class=\"blogsubtitle\">για την εργασία " + row.get("task") + "</div>";
					content += "<div class=\"blogworkdate\">Ημερομηνία εργασίας: " + row.get("dateworked") + "</div>";
					content += "<div class=\"blogworkhours\">Ώρες εργασίας: " + row.get("hours") + "</div>";
					content += "<hr /><div class=\"blogtext\">" + row.get("desc").replaceAll("(\r\n|\n)", "<br />") + " </div>";						
					content += "</div></div>";				
					content += "<div class=\"blogmainbottom\"></div>";
					
					content += "<div class=\"blogaddeddate\"><div class=\"blogaddeddatewrapper\">δημοσιεύθηκε " + row.get("dateadded") +
							" από " + row.get("user") + "</div></div>";		
					
					content += "</div><br /><br />";
				}
				content += "<div class=\"blogpagebuttons\">";
				
				String extraparams = "&amp;uid=" + userID + ((taskID != null)?"&amp;taskid=" + taskID:"");
				
				if(page - 1 >= 0)
					content += "<div class=\"blogbutton\"><a href=\"Blog?page=" + (page - 1) + extraparams + "\">Νεοτερες Αναρτησεις</a></div>";
				
				if(progressList.size() == maxResultsPerPage)
					content += "<div class=\"blogbutton blogolderpage\"><a class=\"\" href=\"Blog?page=" + (page + 1) + extraparams + "\">Παλαιοτερες Αναρτησεις</a></div>";
				content += "</div>";
				content += "<div style=\"clear:both\"></div>";
			}
			

		}
		else
		{
			content += "Πρόβλημα με την φόρτωση του ιστολογίου.";
		}
		
		return HTMLPages.getGeneralPage(content, uid);	
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		if(!LoginCookie.authCookie(request))
			response.sendRedirect("Login");
		else
		{
			String uid = LoginCookie.Get(request)[0];
			
			request.setCharacterEncoding("utf-8");
			String page = request.getParameter("page");
			String userID = request.getParameter("uid");
			String taskID = request.getParameter("taskid");
			
			int pageNumber;
			
			if(page == null) pageNumber = 0;
			if(!Validator.isNumber(page))
				pageNumber = 0;
			else
				pageNumber = Integer.parseInt(page);
			
			response.setContentType("text/html; charset=UTF-8");	
			PrintWriter out = response.getWriter();
			
			out.println(getPage(uid, pageNumber, (userID == null)?uid:userID, taskID));
			
			out.close();			
		}
	}
	
}
